﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Black.Jack
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "Black Jack";
            Program game = new Program();
            game.Run();
        }

        public void Run()
        {
            int choice = 0;

            do
            {
                if (choice == 1)
                {
                    Console.Clear();
                    PlayJackBlack();
                }
                else
                    choice = PrintWelcomeMessage();
            } while (choice != 2);

            Console.Clear();
        }

        public int PrintWelcomeMessage()
	    {
		    Console.Out.WriteLine( " Welcome to JackBlack!"   );
		    Console.Out.WriteLine( "-----------------------"  );
		    Console.Out.WriteLine( "|1. Play a Game\t2.Quit|" );
		    Console.Out.WriteLine( "-----------------------"  );
		    Console.Out.Write( "What would you like to do: "  );

            
            int choice = int.Parse(Console.ReadLine());

		    return choice;
	    }

        public void PlayJackBlack()
        {
		    Deck deck = new Deck();
            deck.Shuffle();
            Hand playerHand = new Hand();
		    Hand dealerHand = new Hand();

            playerHand.AddCard(deck.DealCard());
            dealerHand.AddCard(deck.DealCard());
            playerHand.AddCard(deck.DealCard());
            dealerHand.AddCard(deck.DealCard());

            Console.Out.WriteLine("Player's Hand");
            playerHand.ShowHand(false);

            Console.Out.WriteLine("Dealer's Hand");
            dealerHand.ShowHand(true);

            while (true)
            {
                Console.Out.Write("\nWould you like to hit or hold: ");
                string option = Console.ReadLine();

                if (option.Equals("hit") || option.Equals("Hit"))
                    playerHand.AddCard(deck.DealCard());

                Console.Out.WriteLine("\nPlayer's Hand");
                playerHand.ShowHand(false);

                if (option.Equals("hold") || option.Equals("Hold"))
                    break;
            }

            while (true)
            {
                Console.Out.WriteLine("\nDealer's Hand");
                dealerHand.ShowHand(false);

                if (dealerHand.GetTotal() < 17 & dealerHand.GetTotal() < playerHand.GetTotal())
                    dealerHand.AddCard(deck.DealCard());
                else
                    break;
            }

            if (dealerHand.GetTotal() > 21 || playerHand.GetTotal() == 21 || playerHand.GetTotal() >= dealerHand.GetTotal() && playerHand.GetTotal() < 21)
            {
                Console.WriteLine("\n-------------------------");
                Console.WriteLine("|\tYou Won!\t|");
                Console.WriteLine("-------------------------");
            }
            else
            {
                Console.WriteLine("\n-------------------------");
                Console.WriteLine("|Sorry, maybe next time.|");
                Console.WriteLine("-------------------------");
            }

            Console.Write("\nWould you like to play again [y/n]: ");
            if (Console.ReadLine().Equals("n"))
                Environment.Exit(1);
            else
                Console.Clear();
	    }
    }
}
